package com.test.suan_fa.leecode;

public class 最长回文字段 {

    public static String longestPalindrome(String s) {
        if (s== null || s.length()==0) return "";
        int start=0, end = 0;
        for (int i=0;i<s.length();i++){
            int len1 = expand(s, i-1, i+1);
            int len2 = expand(s, i, i+1);
            int maxL = Math.max(len1, len2);
            if (maxL > end-start){
                start = i-(maxL-1)/2;
                end = i+maxL/2;
            }
        }
        return s.substring(start, end+1);
    }

    public static int expand(String s, int left, int right){
        int L = left;int R = right;
        while (L>=0 && R<s.length() && s.charAt(L) == s.charAt(R)){
            L--;
            R++;
        }
        return R-L-1;
    }


    public static void main(String[] args) {
        System.out.println(longestPalindrome("avbva"));
    }
}